package com.check.operation.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.R;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Order;
import com.check.operation.model.RequestUse;
import com.check.operation.model.User;
import com.check.operation.model.Yzzx;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class YzzxAdapter extends BaseExpandableListAdapter implements OnClickListener{
	private List<Yzzx> yzzxs;
	
	private LayoutInflater layoutInflater;
	PlaySoundPool playSoundPool;
	private User user;
	private String inpationt;
	
	public YzzxAdapter(LayoutInflater layoutInflater,User user) {
		super();
		//this.yzzxs = yzzxs;
		this.layoutInflater=layoutInflater;
		this.user=user;
		
		playSoundPool=new PlaySoundPool(layoutInflater.getContext());
		playSoundPool.loadSfx(R.raw.ok, 1);
		playSoundPool.loadSfx(R.raw.error, 2);
	}
	
	

	@Override
	public int getGroupCount() {
		return yzzxs.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return yzzxs.get(groupPosition).Orders.size();
	}

	@Override
	public Yzzx getGroup(int groupPosition) {
		return yzzxs.get(groupPosition);
	}

	@Override
	public Order getChild(int groupPosition, int childPosition) {
		return yzzxs.get(groupPosition).Orders.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return Long.parseLong(yzzxs.get(groupPosition).GroupID);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return Long.parseLong(yzzxs.get(groupPosition).Orders.get(childPosition).OrderID);
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		ViewHolder holder=new ViewHolder();
		Yzzx yzzx=yzzxs.get(groupPosition);
		holder.yzzx=yzzx;
		View view=layoutInflater.inflate(R.layout.yzzxitem, null);
		holder.fzid=(TextView) view.findViewById(R.id.fzid);
		holder.yzsj=(TextView) view.findViewById(R.id.yzsj);
		holder.yzpc=(TextView) view.findViewById(R.id.yzpc);
		holder.gyfs=(TextView) view.findViewById(R.id.gyfs);
		holder.zxr=(TextView) view.findViewById(R.id.zxr);
		holder.zxsj=(TextView) view.findViewById(R.id.zxsj);
		holder.qrr=(TextView) view.findViewById(R.id.qrr);
		holder.qrsj=(TextView) view.findViewById(R.id.qrsj);
		
		holder.bt1=(Button) view.findViewById(R.id.bt1);
		holder.bt1.setOnClickListener(this);
		holder.bt2=(Button) view.findViewById(R.id.bt2);
		holder.bt2.setOnClickListener(this);
		
		
		holder.bt1.setTag(holder);
		holder.bt2.setTag(holder);
		
		holder.fzid.setText(yzzx.GroupID);
		holder.yzsj.setText(yzzx.OrderTime);
		holder.yzpc.setText(yzzx.FrequName);
		holder.gyfs.setText(yzzx.SupplyName);
		holder.zxr.setText(yzzx.ExecerName);
		holder.zxsj.setText(yzzx.ExecTime);
		holder.qrr.setText(yzzx.CheckerName);
		holder.qrsj.setText(yzzx.CheckTime);
		
		if("true".equals(yzzx.ExecedFlag)){
			holder.bt1.setText("撤销执行");
		}else{
			holder.bt1.setText("执行医嘱");
		}
		
		if("true".equals(yzzx.CheckedFlag)){
			holder.bt2.setText("撤销确认");
		}else{
			holder.bt2.setText("皮试确认");
		}
		
		return view;
	}
	
	static class ViewHolder{
		TextView fzid;
		TextView yzsj;
		TextView yzpc;
		TextView gyfs;
		TextView zxr;
		TextView zxsj;
		TextView qrr;
		TextView qrsj;
		Button bt1;
		Button bt2;
		Yzzx yzzx;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		View view=layoutInflater.inflate(R.layout.yzzxchileditem, null);
		Order order=yzzxs.get(groupPosition).Orders.get(childPosition);
		TextView yzmc=(TextView) view.findViewById(R.id.yzmc);
		TextView gg=(TextView) view.findViewById(R.id.gg);
		TextView ycjl=(TextView) view.findViewById(R.id.ycjl);
		TextView jldw=(TextView) view.findViewById(R.id.jldw);
		TextView zhutuo=(TextView) view.findViewById(R.id.zhutuo);
		
		yzmc.setText(order.OrderName);
		gg.setText(order.Spec);
		ycjl.setText(order.Dose);
		jldw.setText(order.DoseUnit);
		zhutuo.setText(order.Instruction);
		return view;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public List<Yzzx> getYzzxs() {
		return yzzxs;
	}

	public void setYzzxs(List<Yzzx> yzzxs) {
		this.yzzxs = yzzxs;
	}
	
	

	public String getInpationt() {
		return inpationt;
	}

	public void setInpationt(String inpationt) {
		this.inpationt = inpationt;
	}

	private void alert(String message){
		Toast.makeText(layoutInflater.getContext(), message, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onClick(View v) {
		Button button=(Button) v;
		ViewHolder vo=(ViewHolder) v.getTag();
		Yzzx yzzx=vo.yzzx;
		if(v.getId()==R.id.bt1){//执行
			String action=null;
			if("true".equals(yzzx.ExecedFlag)){
				action="order/RevokeExecOrder";
			}else{
				action="order/execorder";
			}
			
		   RequestUse requestUse=new RequestUse();
		   requestUse.user=this.user;
	 	   Map<String,Object> params1=new HashMap<String, Object>();
	 	   params1.put("inpatient_no",inpationt);
	 	   params1.put("act_order_no", yzzx.GroupID);
	 	   requestUse.request=params1;
	 	   String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
	 	   MessagRe<String> res=JSONObject.parseObject(rs, new TypeReference<MessagRe<String>>(){});
	 	   if("success".equals(res.message.code)){
	 		  if("false".equals(yzzx.ExecedFlag)){
	 			  yzzx.ExecedFlag="true";
	 			//  yzzx.ExecerName=this.user.EmpName;
	 			 // yzzx.ExecTime=
	 			  vo.zxr.setText(this.user.EmpName);
	 			  vo.zxsj.setText(JSONUtil.getNYRSFM(new Date()));
	 			  
		 		  button.setText("撤销确认");
		 		  
	 		  }else{
	 			  vo.zxr.setText("");
	 			  vo.zxsj.setText("");
	 			  yzzx.ExecedFlag="false";
		 		  button.setText("执行医嘱");
	 		  }
	 		//  this.notifyDataSetChanged();
	 		  playSoundPool.play(1, 0);
	 	   }else{
	 		  playSoundPool.play(2, 0);
	 	   }
	 	  alert(res.message.text);
		}else if(v.getId()==R.id.bt2){//皮试
			String action=null;
			if("true".equals(yzzx.CheckedFlag)){
				action="order/RevokeSkinTestCheckExecOrder";
			}else{
				action="order/SkinTestCheckExecOrder";
			}
			
		   RequestUse requestUse=new RequestUse();
		   requestUse.user=this.user;
	 	   Map<String,Object> params1=new HashMap<String, Object>();
	 	   params1.put("inpatient_no",inpationt);
	 	   params1.put("act_order_no", yzzx.GroupID);
	 	   requestUse.request=params1;
	 	   String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
	 	   MessagRe<String> res=JSONObject.parseObject(rs, new TypeReference<MessagRe<String>>(){});
	 	   if("success".equals(res.message.code)){
	 		  if("false".equals(yzzx.CheckedFlag)){
	 			  vo.qrr.setText(this.user.EmpName);
	 			  vo.qrsj.setText(JSONUtil.getNYRSFM(new Date()));
	 			  yzzx.CheckedFlag="true";
		 		  button.setText("撤销执行");
	 		  }else{
	 			  vo.qrr.setText("");
	 			  vo.qrsj.setText("");
	 			  yzzx.CheckedFlag="false";
		 		  button.setText("皮试确认");
	 		  }
	 		  //this.notifyDataSetChanged();
	 		  playSoundPool.play(1, 0);
	 	   }else{
	 		  playSoundPool.play(2, 0);
	 	   }
	 	  alert(res.message.text);
			
		}
		
	}

}
